﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Linq.Expressions;
using STHPMS1.Web.W_Model;
using System.Collections.Generic;
using STHPMS1.Common.C_Enum;
using STHPMS1.Common.C_Extension;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/invntory")]
    public class B_InvntoryPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetinvntoryByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;


            var data = this.Scene.Bll.B_Invntory.GetRoadDivisionResult(info);
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("")]
        public void Post(string rdwayid)
        {
            this.Scene.Bll.B_Invntory.DivideRoad(rdwayid);
        }

        [Route("")]
        public void Delete(B_Invntory entity)
        {
            this.Scene.Bll.B_Invntory.Remove(entity);
        }

        /// <summary>
        /// 获取所有路段的id与名称字典 
        /// </summary>
        /// <returns></returns>
        [Route("dict")]
        [HttpPost]
        public dynamic GetDict(string rdwaryid)
        {
            List<string> ids = this.Scene.Bll.B_Invntory.List(rdwaryid);
            if (ids.Count == 0) return "";
            var innerJoinQuery =
                from segid in ids
                select new { id = segid, text = segid };
            return innerJoinQuery;
        }

        /// <summary>
        /// 获取所有路段的id与名称字典 
        /// </summary>
        /// <returns></returns>
        [Route("dictunit")]
        public dynamic GetDictUnit(string rdwaryid)
        {
            List<string> ids = this.Scene.Bll.B_Unit.List(rdwaryid);
            if (ids.Count == 0) return "";
            var innerJoinQuery =
                from segid in ids
                select new { id = segid, text = segid };
            return innerJoinQuery;
        }

        [Route("seg")]
        public IHttpActionResult GetSeg(string rdwaryid=null, int? direction_id = null,int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;


            var data = this.Scene.Bll.B_Invntory.GetSeg(rdwaryid,direction_id,info);
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("unit")]
        public IHttpActionResult GetUnit(string rdwaryid=null, int? direction_id = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;


            var data = this.Scene.Bll.B_Invntory.GetUnit(rdwaryid, direction_id, info);
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }
    }
}